<?php
/**
 * User model service layer
 * 
 * @author Mitchelle Pascual <mitch.pascual@gmail.com>
 * $date 04/01/2010
 *
 */
class Application_Model_Users_DAO extends Owg_Model_BaseDAO implements Owg_Model_IDAO
{
	const FIELD_MAP_ID = 'Id';
	const FIELD_MAP_FIRST_NAME = 'FirstName';
	const FIELD_MAP_LAST_NAME = 'LastName';
	const FIELD_MAP_EMAIL = 'Email';
	const FIELD_MAP_WEB = 'Web';
	
	protected $_dbTable = 'Application_Model_Users_DbTable';
	private static $_self = null;
	
	//implement
	public static function getInstance()
	{
		if (is_null(self::$_self))
				self::$_self = new self;
				
		return self::$_self;
	}
	//implement
	public function save(Owg_Model_BaseModel $p_oUsersModel)
	{
		// because of this http://bugs.php.net/bug.php?id=36601
		// we have to check here that we have the right object model
	   	if ( ($p_oUsersModel instanceof Application_Model_Users_Model) == false )
   		{
   			throw new Exception('Application_Model_Users_DAO save exception');
   		}
   		
   		
	    $aData = array(
            'first_name'=> $p_oUsersModel->getFirstName(),
	    	'last_name'	=> $p_oUsersModel->getLastName(),
	    	'email' 	=> $p_oUsersModel->getEmail(),
	    	'web' 		=> $p_oUsersModel->getWeb()
        );
		
        if ($p_oUsersModel->getId() == 0) 
        {
            parent::getDbTable()->insert($aData);
        } 
        else 
        {
            parent::getDbTable()->update($aData, array('id = ?' => $p_oUsersModel->getId()));
        }
   		
   		
	}
	//implement
	public function delete($p_iId)
	{
		$where = self::_getWhereClause(self::FIELD_MAP_ID, $p_iId);
   		return $this->getDbTable()->delete($where);
	}
	//implement
	public function getById($p_iId)
	{
		
	}
	//implement
	public function findList($p_iOffset = 0, $p_iLimit = 0, $p_sOrder = '', 
		$p_bAsc = true, $p_aWhere = array()){
			
		}
	
		
	//override	
	protected function _getWhereClause($p_sFilterField, $p_mFilterValue = '', 
		$p_sFilterCondition = Owg_Model_BaseDAO::SQL_CONDITION_EQUALS, $p_sFilterOperator = '')
		{
	    	switch($p_sFilterField)
	    	{
	    		case self::FIELD_MAP_ID:
	    			$sWhereClause = $p_sFilterOperator . ' id ' . $p_sFilterCondition . ' ' . 
	    				$this->getDbTable()->getDefaultAdapter()->quote($p_mFilterValue, Zend_Db::INT_TYPE) . ' ';
	    			break;
	
	    		default:
	    			$sWhereClause = null;
	    			break;
	    	}
	    	
	    	return $sWhereClause;
		}	
		
		
	protected function _getOrderClause($p_sOrder, $p_bAsc = true)
	{
		$sOrderClause = '';
		
		if (!empty($sSort))
		{
			if ($p_bAsc)
			{
				$sSort = 'ASC';
			}
			else 
			{
				$sSort = 'DESC';
			}
		}
		
		switch ($p_sOrder)
		{
			case self::FIELD_MAP_ID:
				$sOrderClause = self::getRealColumnName(self::FIELD_MAP_ID) . ' ' . $sSort;
				break;
			case self::FIELD_MAP_FIRST_NAME:
				$sOrderClause = self::getRealColumnName(self::FIELD_MAP_FIRST_NAME) . ' ' . $sSort;
				break;
			case self::FIELD_MAP_LAST_NAME:
				$sOrderClause = self::getRealColumnName(self::FIELD_MAP_LAST_NAME) . ' ' . $sSort;
				break;
			case self::FIELD_MAP_EMAIL:
				$sOrderClause = self::getRealColumnName(self::FIELD_MAP_EMAIL) . ' ' . $sSort;
				break;
			case self::FIELD_MAP_WEB:
				$sOrderClause = self::getRealColumnName(self::FIELD_MAP_WEB) . ' ' . $sSort;
				break;				
			default:
				$sOrderClause = '';
				break;
		}
		
		return $sOrderClause;
	}
	
	
	public static function getRealColumnName($p_sColumn)
	{
		$sColumn = '';
		switch ($p_sColumn)
		{
			case self::FIELD_MAP_ID:
				$sColumn = 'id';
				break;
			case self::FIELD_MAP_FIRST_NAME:
				$sColumn = 'first_name';
				break;
			case self::FIELD_MAP_LAST_NAME:
				$sColumn = 'last_name';
				break;
			case self::FIELD_MAP_EMAIL:
				$sColumn = 'email';
				break;
			case self::FIELD_MAP_WEB:
				$sColumn = 'web';
				break;
			default:
				$sColumn = '';
				break;
		}
		
		return $sColumn;
	}
		
}